<?php
$data=$_POST;
if($data){
	set_time_limit(0);
	ini_set('memory_limit', '-1');
	function getDir($path,$data){
		static $arr=[];
		if(is_dir($path)){
			$dir = scandir($path);
			foreach ($dir as $value){
				$sub_path =$path .'/'.$value;
				if($value == '.' || $value == '..'){
					continue;
				}else if(is_dir($sub_path)){
					getDir($sub_path,$data);
				}else{
					$wo=$path. '/'.$value;
					if(in_array(get_extension($wo),$data)){
						$arr[]=$wo;
					}
				}
			}
		}
		return $arr;
	}
	function get_extension($file){
		return pathinfo($file)['extension'];
	}
	
	$data=array_keys($data['like']);
	$text=__DIR__."/data.txt";
	$data_wan = __DIR__."/data_wan.txt";
	if(file_exists($text)){
	    unlink($text) or die("删除文件失败，请检查文件夹权限");;
	}
	if(file_exists($data_wan)){
	    unlink($data_wan);
	}
	$arr=getDir(__DIR__,$data);
	
	$myfile = fopen($text, "w+") or die("写入文件失败，请检查文件夹权限");
	
	foreach($arr as $v){
		$txt=file_get_contents($v);
		if($txt){
			fwrite($myfile,trim($txt));
		}
	}
	fclose($myfile);
	
	$line = count(file($text));
	
	if($line>3000){
		$myfile = fopen($data_wan, "w+") or die("写入文件失败，请检查文件夹权限");
		$handle = fopen($text,"r");//以只读方式打开一个文件
		$i = 0;
		while(!feof($handle)){//函数检测是否已到达文件末尾
			if($wo=fgets($handle)){// 从文件指针中读取一行
				if($i<1500 or $i>=$line-1500){
					fwrite($myfile, $wo);
				}
				$i++;
			};
		}
		fclose($handle);
		fclose($myfile);
	}else{
		$myfile = fopen($data_wan, "w+") or die("写入文件失败，请检查文件夹权限");
		$handle = fopen($text,"r");//以只读方式打开一个文件
		$i = 0;
		while(!feof($handle)){//函数检测是否已到达文件末尾
			if($wo=fgets($handle)){// 从文件指针中读取一行
				fwrite($myfile, $wo);
				$i++;
			};
		}
		fclose($handle);
		fclose($myfile);
	}
	
	
	
}
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>php编写文件合并</title>
		<link crossorigin="anonymous" integrity="sha512-gK5o6RvUyTWSY+nO4Q9kJKGXbffUbV+u/R3bOAnCiOSIGt8GNDkvLvsQC2WaxyIQwGS56zpwt1TajavwKXBwKA==" href="https://lib.baomitu.com/layui/2.6.8/css/layui.css" rel="stylesheet">
	</head>
	<body style="width: 800px;margin: 50px auto;">
		<h1 style="text-align: center;margin: 50px;">申请软著是使用，统计代码</h1>
		<blockquote class="layui-elem-quote">请把代码放在code文件内，然后请选择文件后缀，后提交。请注意文件夹的读写权限！！！完成后会生成data.txt（完整所有代码），和data_wan.txt（拿出3000行,不足3000将拿出全部！）。<?php if($_POST){ ?> <span style="color: red;">总代码量，共<?php echo $line;?>行</span> <?php } ?></blockquote>
		
		<form class="layui-form" method="post" action="">
		  <div class="layui-form-item">
		    <label class="layui-form-label">请选择</label>
		    <div class="layui-input-block">
		      <input type="checkbox" name="like[js]" title=".js" checked>
		      <input type="checkbox" name="like[vue]" title=".vue" checked>
		      <input type="checkbox" name="like[php]" title=".php">
			  <input type="checkbox" name="like[css]" title=".css">
			  <input type="checkbox" name="like[html]" title=".html">
			  <input type="checkbox" name="like[json]" title=".json">
		    </div>
		  </div>
		  <div class="layui-form-item">
		    <div class="layui-input-block">
		      <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
		    </div>
		  </div>
		</form>
		 <script crossorigin="anonymous" integrity="sha512-EKrFvch3qTzLFQgjbcjpsRmF8T3UCtc9ojtMAu6dvvP+bV8qYUDOaQ84nwYCkSLT7lbqGoya/Kf+8fyCBE0vRg==" src="https://lib.baomitu.com/layui/2.6.8/layui.min.js"></script>
		<script>
		//Demo
		layui.use('form', function(){
		  var form = layui.form;
		  //监听提交
		  form.on('submit(formDemo)', function(data){
		    //layer.msg(data.field);
			console.log(data);
		    //return false;
		  });
		});
		</script>
		      
	</body>
</html>
